package ${scfg._biz_pkg};

import ${scfg._api_pkg}.I${tcfg.tableNameFormat!}Service;
import ${scfg._dao_pkg}.I${tcfg.tableNameFormat!}${scfg._dao_suffix!};
import ${scfg._domain_pkg}.${tcfg.tableNameFormat!};
import ${scfg._dto_pkg}.${tcfg.tableNameFormat!}Dto;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.wu.framework.ResponseDto;
import cn.wu.framework.SysErrorCode;
import cn.wu.framework.annotation.FeignService;
import cn.wu.framework.exception.ServiceException;
import cn.wu.framework.service.BaseService;
import cn.wu.framework.util.CommonConstant;
import cn.wu.framework.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@FeignService
@Slf4j
public class ${tcfg.tableNameFormat!}Service extends BaseService implements I${tcfg.tableNameFormat!}Service {
    @Autowired
    I${tcfg.tableNameFormat!}${scfg._dao_suffix!} ${tableNameL}${scfg._dao_suffix!};

    @[ if ("1"==scfg._insert||"1"==scfg._update) { /]
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public ResponseDto saveOrUpdateData(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        ResponseDto result = new ResponseDto(0,"success");
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);

            if (@[ for(t in pks){ /]entity.get${strUtil.toUpperCase(t.columnNameFormat,'_')}()!=null @[ } /] && ${tableNameL}${scfg._dao_suffix!}.isDataHasExist(entity) != 0) {
                 @[ if ("1"==scfg._update){ /]${tableNameL}${scfg._dao_suffix!}.update(entity);@[ } /]
            } else {
                if(entity.getId()==null){
                    entity.setId(IdUtil.nextId());
                }
                @[ if ("1"==scfg._insert){ /]${tableNameL}${scfg._dao_suffix!}.insert(entity);@[ } /]
                result.data=entity.getId();
            }
        } catch (Exception e) {
            log.error("信息保存异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return result;
    }
    @[ } /]

    @[ if ("1"==scfg._del) { /]
    @Override
    public String deleteData(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        String result = "success";
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
            if(${tableNameL}${scfg._dao_suffix!}.deleteByPrimaryKey(entity)==0){
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("物理删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return result;
    }
    @[ } /]

    @[ if ("1"==scfg._delLogic && null!=scfg._col_del) { /]
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public String deleteDataById(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        String result = "success";
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
            if(${tableNameL}${scfg._dao_suffix!}.deleteById(entity)==0){
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("逻辑删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return result;
    }
    @[ } /]

    @[ if ("1"==scfg._page) { /]
    @Override
    public PageInfo findDataIsPage(${tcfg.tableNameFormat!}Dto dto) throws Exception {
       PageInfo pageInfo=null;
       try {
           ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
           PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
           List list = ${tableNameL}${scfg._dao_suffix!}.findDataIsPage(entity);
           pageInfo=new PageInfo(list);
           pageInfo.setList(copyTo(pageInfo.getList(), ${tcfg.tableNameFormat!}Dto.class));
       } catch (Exception e) {
           log.error("信息[分页]查询异常!", e);
           throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
       }
       return pageInfo;
    }
    @[ } /]

    @[ if ("1"==scfg._list||"1"==scfg._tree) { /]
    @Override
    public List<${tcfg.tableNameFormat!}Dto> findDataIsList(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        List<${tcfg.tableNameFormat!}Dto>  results = null;
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
             results = copyTo(${tableNameL}${scfg._dao_suffix!}.findDataIsList(entity), ${tcfg.tableNameFormat!}Dto.class);
        } catch (Exception e) {
            log.error("信息[列表]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return  results;
    }
     @[ } /]

    @[ if ("1"==scfg._detail) { /]
    @Override
    public ${tcfg.tableNameFormat!}Dto findDataById(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        ${tcfg.tableNameFormat!}Dto result = null;
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
            result = copyTo(${tableNameL}${scfg._dao_suffix!}.selectByPrimaryKey(entity),${tcfg.tableNameFormat!}Dto.class);
        } catch (Exception e) {
            log.error("信息[详情]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return result;
    }
    @[ } /]

    @[ if ("1"==scfg._recovery && null!=scfg._col_del) { /]
    @Override
    public String recoveryDataById(${tcfg.tableNameFormat!}Dto dto) throws Exception {
        String result = "success";
        try {
            ${tcfg.tableNameFormat!} entity = copyTo(dto, ${tcfg.tableNameFormat!}.class);
            if(${tableNameL}${scfg._dao_suffix!}.recoveryDataById(entity)==0){
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("数据恢复异常!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
        return result;
    }
    @[ } /]

    @[ if ("1"==scfg._tree) { /]
    @Override
    public List<${tcfg.tableNameFormat!}Dto> findDataIsTree(@RequestBody(required = false) ${tcfg.tableNameFormat!}Dto dto) {
        try {
            List<${tcfg.tableNameFormat!}Dto> results = findDataIsList(dto);
            if (results == null) {
                return null;
            }
            NodeTree<${tcfg.tableNameFormat!}Dto> tree = new NodeTree(results, "id", "parentId", "nodes");
            return tree.buildTree();
        } catch (Exception e) {
            log.error("信息树获取失败!", e);
            throw new ServiceException(SysErrorCode.defaultError,e.getMessage());
        }
    }
    @[ } /]
}
